// JavaScript Document
var BASE_URL_HOST = location.host;
var BASE_URL = "https://"+BASE_URL_HOST;
var channel = "";
function isAndroid() {
	return /(Android)/i.test(navigator.userAgent);
}

function isApple() {
	return /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent);
}
function isSony() {
	return /sonyapp/i.test(navigator.userAgent)
}
function isSonyAppwk() {
	return /sonyapp_wkwebview/i.test(navigator.userAgent)
}
function isSonyApp() {
	return isSony() || isSonyAppwk();
}

var ajaxfunc = function (url, param, type, callback) {
	$.ajax(
		{
			url: url,
			type: type,
			data: JSON.stringify(param),
			contentType: "application/json",
			dataType: "json",
			/*xhrFields: {
				withCredentials: true
			},
			beforeSend: function(xhr) {
					xhr.withCredentials = true;
				},*/
			cache: false,
			success: function (data) {

				callback(data);

			},
			error: function (error) {
				callback(JSON.parse(error.responseText));


			}

		})
}






// 如果App进入-交互
function connectWebViewJavascriptBridge(callback) {

	if (window.WebViewJavascriptBridge) {

		callback(WebViewJavascriptBridge)
	} else {
		document.addEventListener('WebViewJavascriptBridgeReady', function () {
			callback(WebViewJavascriptBridge)
		}, false)
	}
}
function doAppAction(ed) {
	try {
		if (isApple()) {
			if (isSonyAppwk()) {
				window.webkit.messageHandlers.showName.postMessage(ed);
			} else if (isSony()) {
				connectWebViewJavascriptBridge((bridge) => {
					bridge.send(JSON.stringify(ed), function (response) {
						console.log(response);
					})
				});
			}
		}
		if (isAndroid() && ed.linktype) {
			window.SysClientJs.linktype(ed.linktype, JSON.stringify(ed));
		}
	} catch (err) {
		console.log("do app linktype", err);
	}
}

function toLogin() {

	if (isSonyApp()) {
		// var _wref = location.href;
		// if(_wref.lastIndexOf('?')>-1){
		// 	_wref = _wref + '&t='+Math.random();
		// }else{
		// 	_wref = _wref + '?t='+Math.random();
		// }
		var whref = encodeURIComponent(window.location.href);
		var getapp = { "link_type": "2", "link": BASE_URL+"/content/dam/sonystyle-club/index.html#/?fromapp=app&linkFrom=999&redirect=" + whref, "linktype": "2", "titleName": "索尼中国" }
		doAppAction(getapp);
	} else {

		if (channel == "WECHAT") {
				
			var whref = window.location.href;
			localStorage.setItem("backurl", whref);
			window.location.href = BASE_URL+"/content/dam/sonystyle-club/index.html#/login?mini=true";

			
		//wx.miniProgram.getEnv((res) => {
		//		if (res.miniprogram) {
		//			var links = window.location.href;
		//			wx.miniProgram.navigateTo({
		//				url: '/pages/index/index?currentTab=3&backurl=' + links
		//			})
		//		} else {
		//			var whref = window.location.href;
		//			localStorage.setItem("backurl", whref);
		//			window.location.href = "/content/dam/sonystyle-club/index.html#/login";
//
//				}
//			})
		} else {
			var whref = window.location.href;
			localStorage.setItem("backurl", whref);
			window.location.href = BASE_URL+"/content/dam/sonystyle-club/index.html#/login";


		}

	}
}



function toRegister() {
	var whref = window.location.href;
	localStorage.setItem("backurl", whref);

	if (isSonyApp()) {
		var whref = encodeURIComponent(window.location.href);
		var getapp = { "link_type": "2", "link": BASE_URL+"/content/dam/sonystyle-club/index.html#/?fromapp=app&linkFrom=999&redirect=" + whref, "linktype": "2", "titleName": "索尼中国" }
		doAppAction(getapp);
	} else {

		if (channel == "WECHAT") {
			wx.miniProgram.getEnv((res) => {
				if (res.miniprogram) {
					var links = window.location.href;
					wx.miniProgram.navigateTo({
						url: '/pages/index/index?currentTab=3&backurl=' + links
					})
				} else {
					var whref = window.location.href;
					localStorage.setItem("backurl", whref);
					window.location.href = BASE_URL+"/content/dam/sonystyle-club/index.html#/register";
					

				}
			})
		} else {
			var whref = window.location.href;
			localStorage.setItem("backurl", whref);
			window.location.href = BASE_URL+"/content/dam/sonystyle-club/index.html#/register";
			

		}


	}

}


function gotoLinks(pid) {
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {

		wx.miniProgram.getEnv((res) => {

			if (res.miniprogram) {

				wx.miniProgram.navigateTo({ url: '/packageProduct/pages/product/product?eightd=' + pid });

			}
		})
	} else {
		if (isSonyApp()) {
			var ed = {};
			ed.link = window.location.origin + BASE_URL_HOST + "/content/dam/sonystyle-wechat/index.html#/Product?eightD=" + pid;
			//ed.link = window.location.origin + pid
			ed.title = "产品详情";
			ed.linktype = '4';
			doAppAction(ed);
		} else {
			window.location.href = window.location.origin + BASE_URL_HOST + "/content/dam/sonystyle-wechat/index.html#/Product?eightD=" + pid;

		}

	}
}




function mshareTowechat(title, description, imagepath, links) {
	if (isSonyApp()) {
		var getapp = { "linktype": "32", "shareTitle": title, "shareImage": imagepath, "shareContent": description, "shareURL": links }
		doAppAction(getapp);
	}

}




if (isSonyApp()) {
	var getapp = { linktype: '27' }
	doAppAction(getapp);

	var getapp1 = { linktype: '77' }
	doAppAction(getapp1);
}

if (isSonyApp()) {
	channel = 'APP';
} else {
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == "micromessenger") {
		channel = 'WECHAT';
	} else {
		channel = 'WAP';
	}
}



// JavaScript Document
(function (common) {
	var space = {};
	//body
	space.ajaxfunc1 = function (url, param, type, callback) {
		$.ajax(
			{
				url: url,
				type: type,
				data: JSON.stringify(param),
				contentType: "application/json",
				dataType: "json",
				beforeSend: function () { },
				cache: false,
				success: function (data) {
					callback(data);

				},
				error: function (error) {
					callback(JSON.parse(error.responseText));


				}

			})
	}
	//formdata
	space.ajaxfunc = function (url, param, type, callback) {
		$.ajax(
			{
				url: url,
				type: type,
				data: param,
				//contentType: "text/html;charset=UTF-8",
				dataType: "json",
				beforeSend: function () { },
				cache: false,
				success: function (data) {
					callback(data);

				},
				error: function (error) {
					callback(JSON.parse(error.responseText));


				}

			})
	}

	space.ajaxfunc2 = function (url, param, type, callback) {
		$.ajax(
			{
				url: url,
				type: type,
				contentType: "application/json",
				beforeSend: function (xhr) {
					xhr.setRequestHeader("Authorization", "Bearer " + param);
				},
				dataType: "json",
				cache: false,
				success: function (data) {
					callback(data);

				},
				error: function (error) {
					callback(JSON.parse(error.responseText));


				}

			})
	}

	common.space = space;
})(this)
function toast(message,callback) {
	var $dialog = $('#alert');
	$("#alertContent").html(message);
	$dialog.show();
	$dialog.find('.weui-dialog__btn').on('click', function () {
		$dialog.hide();
		if(callback){callback()}
	});
}

$.getUrlParam = function (name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]); return null;
}

function showDialog(id){
	let $id = $("#"+id);
	$id.show();

}

function hideDialog(id){
	let $id = $("#"+id);
	$id.hide();

}

getqs = function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]); return '';
}




$.extend({
	tmpl:function(id,data){
		var tmpls = $("#"+id).html();
		return doT.template(tmpls).apply(null,[data]);
	
		}
	})
	
let userId = "";
let xchannel = "";
let logined = true;
if (isSonyApp()) {

	
	window['pushUserInfo'] = function (sendMsg) {
		console.log("H5获得APP UserInfo:" + sendMsg);
		var send = JSON.parse(sendMsg);
		localStorage.setItem("extendMsg", sendMsg)
		if (send && send.access_token) {
			localStorage.setItem("access_token", send.access_token);
			localStorage.setItem("member_group", send.customerGroup);
		} else {
			console.log("SendMsg 有错误:" + sendMsg);
		}
		localStorage.setItem("reviceAppInfo","true");


		// itoast(JSON.stringify(send))
	}

		
	var getapp = { "link_type": "27", "link": BASE_URL+"/content/dam/sonystyle-club/index.html#/?fromapp=app&linkFrom=999&redirect=" + location.href, "linktype": "27", "titleName": "索尼中国" }
	doAppAction(getapp);
}



	

